//
//  ZJNotification.m
//  MoreStarLiveTelecast
//
//  Created by babbage on 16/8/23.
//  Copyright © 2016年 babbage. All rights reserved.
//

#import "ZJNotification.h"
#import "LoginVC.h"

@interface ZJNotification ()<CLLocationManagerDelegate>

@property (nonatomic,strong) NSTimer *myRegistTimer;//倒计时

@property (nonatomic,strong) NSTimer *myFindPWDTimer;//倒计时

@property(nonatomic,retain) CLLocationManager *locationManager;//定位

@end

@implementation ZJNotification

+ (ZJNotification *)shareCenter
{
    static ZJNotification *_instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        if (!_instance) {
            _instance = [[self alloc]init];
        }
    });
    
    return _instance;
}
- (instancetype)init
{
    if (self = [super init]) {
        
    }
    return self;
}
#pragma mark --- 注册通知 ---
-(void)createAllNotifacation
{
    [GJCFNotificationCenter addObserver:self selector:@selector(userWantLogin:) name:UserWantLogin object:nil];
    
    [GJCFNotificationCenter addObserver:self selector:@selector(userLoginSuccess:) name:UserLoginSuccess object:nil];
    
    [GJCFNotificationCenter addObserver:self selector:@selector(startRegistTimer:) name:StarRegistTimer object:nil];
    
    [GJCFNotificationCenter addObserver:self selector:@selector(startfindPWDTimer:) name:StarFindPWDTimer object:nil];
    
    [GJCFNotificationCenter addObserver:self selector:@selector(locationStart) name:StarLocationService object:nil];//定位
    [self locationStart];//程序运行进行定位 从后台进入前台进行定位
}
#pragma mark --- 触发必须登录的事件 用户没有登录 通知登录 ---
-(void)userWantLogin:(NSNotificationCenter *)sender
{
    LoginVC *loginVC = [[LoginVC alloc] init];
    UIViewController *superVC = [sender valueForKey:@"object"];
    [superVC presentViewController:loginVC animated:YES completion:^{
        
    }];
}

#pragma mark --- 登录成功之后会走这个方法 ---
-(void)userLoginSuccess:(NSNotificationCenter *)sender
{
    //登录成功后应该刷新
}

#pragma mark --- 注册开始计时 ---
-(void)startRegistTimer:(NSNotificationCenter *)sender
{
    [self.myRegistTimer setFireDate:[NSDate distantPast]];//打开定时器
}

#pragma mark --- 注册倒计时 ---
-(NSTimer *)myRegistTimer
{
    if (!_myRegistTimer) {
        _myRegistTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(registTime:) userInfo:nil repeats:YES];
    }
    return _myRegistTimer;
}
-(void)registTime:(NSTimer *)timer
{
    static int i = 60;
    [GJCFNotificationCenter postNotificationName:RegistNowTimer object:nil userInfo:@{@"registTime":@(i)}];
    if (i == 0) {
        i = 60;
        [self.myRegistTimer setFireDate:[NSDate distantFuture]];//关闭定时器
    }
    i --;
}
#pragma mark --- 找回密码开始计时 ---
-(void)startfindPWDTimer:(NSNotificationCenter *)sender
{
    [self.myFindPWDTimer setFireDate:[NSDate distantPast]];//打开定时器
}

#pragma mark --- 找回密码倒计时 ---
-(NSTimer *)myFindPWDTimer
{
    if (!_myFindPWDTimer) {
        _myFindPWDTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(findPWDTime:) userInfo:nil repeats:YES];
    }
    return _myFindPWDTimer;
}
-(void)findPWDTime:(NSTimer *)timer
{
    static int i = 60;
    [GJCFNotificationCenter postNotificationName:FindPWDNowTimer object:nil userInfo:@{@"findPWDTime":@(i)}];
    if (i == 0) {
        i = 60;
        [self.myFindPWDTimer setFireDate:[NSDate distantFuture]];//关闭定时器
    }
    i --;
}

#pragma mark --- 定位 ---
#pragma mark LocationStart
-(void)locationStart{
    // 判断定位操作是否允许
    if ([CLLocationManager locationServicesEnabled]) {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        //设置定位精度
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = kCLLocationAccuracyHundredMeters;//每隔多少米定位一次
        if (iOS8) {
            //使用应用程序期间允许访问位置数据
            [self.locationManager requestWhenInUseAuthorization];
        }
        //开始定位
        [self.locationManager startUpdatingLocation];
    }else{
        //提示用户无法定位操作系统
        NSLog(@"%@",@"定位服务当前不可用尚未打开，请设置打开！");
    }
}
#pragma mark - CoreLocation Delegate

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{
    //系统会一直更新数据，直到选择停止更新，因为我们只需要获得一次经纬度即可，所以获取之后就停止更新
    [self.locationManager stopUpdatingLocation];
    //此处locations存储了持续更新的位置坐标值，取最后一个值为最新位置，如果不想让其持续更新位置，则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
    CLLocation *currentLocation = [locations lastObject];
    //这个是给模型赋值
    [BMKUserData userData].localCoordinate = currentLocation.coordinate;
    //获取当前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //根据经纬度反向地理编译出地址信息
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error)
     {
         if (array.count >0)
         {
             CLPlacemark *placemark = [array objectAtIndex:0];
             //获取城市
             NSString *currCity = placemark.locality;
             if (!currCity) {
                 //四大直辖市的城市信息无法通过locality获得，只能通过获取省份的方法来获得（如果city为空，则可知为直辖市）
                 currCity = placemark.administrativeArea;
             }
             
         } else if (error ==nil && [array count] == 0)
         {
             NSLog(@"No results were returned.");
         }else if (error !=nil)
         {
             NSLog(@"An error occurred = %@", error);
         }
         
     }];
    
}
- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
    if (error.code ==kCLErrorDenied) {
        // 提示用户出错原因，可按住Option键点击 KCLErrorDenied的查看更多出错信息，可打印error.code值查找原因所在
    }
    
}

@end
